package org.jeecg.modules.basic.entry.controller;


import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.basic.demo.student.entity.LwgyBaseStudent;
import org.jeecg.modules.basic.entry.entity.LwgyInoutRecord;

import org.jeecg.modules.basic.entry.service.ILwgyInoutRecordService;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;


import java.util.List;
import java.util.Map;

/**
 * @Description: RabbitMQ消费者
 * @author: sn
 * @date: 2021年02月22日 15:36
 */
@Slf4j
@Component
public class OrderReceiver {
    @Autowired
    private ILwgyInoutRecordService lwgyInoutRecordService;



    /**
     * 人脸识别进出数据，实现人来开电人走断电
     * */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "order-queue",durable = "true"),
            exchange = @Exchange(name="order-exchange",durable="true",type="topic"),
            key="order.*"
        )
    )
    public void onOrderMessage(
            @Payload LwgyInoutRecord lwgyInoutRecord,
            @Headers Map<String ,Object> headers,
            Channel channel
    ){
        log.info("消费者接收到一条消息："+lwgyInoutRecord);
        try{
        //消费者
           // 消费者接收到一条消息：LwgyInoutRecord
            // (id=2c9191717f496020017f49624fb90016,
            // guestId=1618, userId=20200209023, userName=徐圣楠,
            // direction=1, type=1, receiptId=null, receiptName=null,
            // equipment=null, recordDate=Wed Mar 02 14:47:33 CST 2022,
            // headImg=B1202203021498912881480871936, mode=1, idCard=null,
            // location=8A, temperature=null, ifMask=null, abnormalType=0,
            // remark=null)
            lwgyInoutRecordService.save(lwgyInoutRecord);
            lwgyInoutRecordService.powerController(lwgyInoutRecord);

            //---------------------与电控联动---------------------
            //宿舍有人有点无人无电
            //按照学生宿舍中学生进出情况判断开断电
          /*  String stuid = lwgyInoutRecord.getUserId();//系统中的id
            //根据id获取住宿信息,如果为空，考虑是老师、其他人员或者无住宿的学生
            List<Map<String, String>> stuFj = iLwgyInoutRecordService.getStudentFjById(stuid);
            if(stuFj.size()>0){
                String direction = lwgyInoutRecord.getDirection();//1进2出0无
                lwgyInoutRecordService.stuBindPower(stuid,direction,stuFj);
            }*/
            Long deliveryTag =(Long) headers.get(AmqpHeaders.DELIVERY_TAG);
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            log.error(e.getMessage(), e);
        }
    }
}
